package com.hyt.it.ogt.kq.main.controller.bm.stcenter;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.core.util.FastJsonUtil;
import com.hyt.it.ogt.kq.common.bm.utils.Constant;
import com.hyt.it.ogt.kq.main.controller.base.BaseController;
import com.hyt.it.ogt.kq.service.bm.model.entity.StudentPayInfo;
import com.hyt.it.ogt.kq.service.bm.model.vo.PayVo;
import com.hyt.it.ogt.kq.service.bm.model.vo.SubjectPayRecordVO;
import com.hyt.it.ogt.kq.service.bm.service.stcenter.IStcenterStudentPayService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;

/**
 * @Description:
 * @Author: STARF
 * @CreateDate: 2022/05/17 17:25
 **/
@RestController
@Api(tags = "25.考生服务中心：支付相关接口", value = "考生支付相关接口")
@ApiSort(value = 25)
@Slf4j
@RequestMapping("/bm/studentPay")
public class StcenterStudentPayController extends BaseController{

    @Resource
    private IStcenterStudentPayService iStudentPay2Service;

    @ApiOperation(value = "25.1.1 查询订单信息", notes = "20000:成功", response = StudentPayInfo.class)
    @ApiOperationSort(value = 1)
    @GetMapping(value = "/getStudentPayInfo")
    public Object getStudentPayInfo(String studentId) {
        StudentPayInfo studentPayInfo = null;
        try {
            studentPayInfo = iStudentPay2Service.getStudentPayInfo(studentId);
        } catch (Exception e) {
            if ("15038".equals(e.getMessage())) {
                return ApiResponse.builder().code(Constant.RESPONSE_CODE_31010172).data("考生60秒内重复缴费，请勿重新提交！").build();
            }
            if ("15046".equals(e.getMessage())) {
                return ApiResponse.builder().code(Constant.RESPONSE_CODE_31010173).data("该项目设置不需要缴费！").build();
            }
            if ("15044".equals(e.getMessage())) {
                return ApiResponse.builder().code(Constant.RESPONSE_CODE_31010174).data("考生暂无需缴费科目，请勿重复缴费！").build();
            }
            if ("15048".equals(e.getMessage())) {
                return ApiResponse.builder().code(Constant.RESPONSE_CODE_31010175).data("您报名的科目暂未通过审核，请等待通过审核再进行缴费！").build();
            }
        }
        return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).data(studentPayInfo).build();
    }


    @ApiOperation(value = "25.1.2 支付：跳转第三方支付页面进行支付(国内支付)", notes = "20000:成功")
    @ApiOperationSort(value = 2)
    @PostMapping(value = "/topay")
    public Object topay(@RequestBody PayVo payVo) {
    	log.info("## topay payVo:{}", FastJsonUtil.getBeanToJson(payVo));
        Map<String, Object> payMap = iStudentPay2Service.topay(payVo);
        return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).data(payMap).build();
    }

    @ApiOperation(value = "25.1.3 查询考生科目的缴退费记录", notes = "20000:成功", response = SubjectPayRecordVO.class)
    @ApiOperationSort(value = 1)
    @GetMapping(value = "/querystudentSubjectPayRecord")
    public Object querystudentSubjectPayRecord(@RequestParam String studentId) {
        return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).data(iStudentPay2Service.querystudentSubjectPayRecord(studentId, null)).build();
    }

    @ApiOperation(value = "25.1.3 查询订单是否超时", notes = "20000:成功")
    @ApiOperationSort(value = 1)
    @GetMapping(value = "/orderHasOverTime")
    public Object orderHasOverTime(@RequestParam String studentId, @RequestParam String subjectIds) {
        try {
            return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).data(0).build();
        }catch (Exception e) {
            log.error(e.getMessage());
        }
        return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).data(0).build();
    }

    @ApiOperation(value = "25.1.3 考生报名科目是否需要缴费: 0-不需要，1-需要", notes = "20000:成功")
    @ApiOperationSort(value = 1)
    @GetMapping(value = "/studentProjectNeedPay")
    public Object studentProjectNeedPay(String studentId) {
        if (StringUtils.isEmpty(studentId)) {
            return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).data(0).build();
        }
        try {
            boolean needPay = iStudentPay2Service.studentProjectNeedPay(studentId);
            return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).data(needPay ? 1 : 0).build();
        }catch (Exception e) {
            log.error(e.getMessage());
        }
        return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).data(0).build();
    }
}
